package Offer008;

/**
 * 和大于 等于target的最短子数组
 * @author 23737
 * @time 2021.12.7
 */
public class Test {
    public static void main(String[] args) {

    }
}

/**
 * 滑动窗口
 */
class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int n = nums.length;
        if(n == 0){
            return 0;
        }
        int start = 0;
        int end = 0;
        int sum = 0;
        int res = Integer.MAX_VALUE;
        while(end < n){
            sum += nums[end];
            while(sum >= target){
                res = Math.min(res,end - start + 1);
                sum -= nums[start];
                start ++;
            }
            end ++;
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }
}
